var UserTutorindex = app.controller('UserTutorindex', function($rootScope,$stateParams, $scope, $http,$state,$timeout,$cordovaDevice,$cordovaChat,$route,UserTutorindexData) {
	try{
		try{
			Wechat.isInstalled({scope: 'snsapi_userinfo'}, 
					function onSuccess(suc) {
				$rootScope.inswx=true;
			},
			function onError(err) {
				$rootScope.inswx=false;
			}
			);

			YCQQ.checkClientInstalled(
					function onSuccess(suc) {
						$rootScope.insqq=true;
					},
					function onError(err) {
						$rootScope.insqq=false;
					}
			);
		}catch (e) {
		}
		
		$rootScope.showf=false;

		$scope.info={};
		$scope.goodat={};
		$scope.consult={};
		$scope.eva={};
		$scope.show=true;
		$scope.more="更多 >";


		bannner_height("tutor_header",32/36);

		var url=SITE_URL+"index.php?app=baseloginapp&act=t_view&callback=JSON_CALLBACK&id="+$stateParams.id;
		$http.jsonp(url).success( function(data) {
			if(data.done){
			}
		});	

		var url=SITE_URL+"index.php?app=baseloginapp&act=tutorindex&callback=JSON_CALLBACK&id="+$stateParams.id;
		$http.jsonp(url).success( function(data) {
			if(data.done){
				$scope.info=data.retval.info;
				$scope.goodat=data.retval.goodat;
				$scope.consult=data.retval.consult;
				$scope.eva=data.retval.eva;
			}
		});	


		//关注与取消关注操作
		$scope.follow=function(){
			if($scope.info!=null){
				if($scope.info.f!=null){
					if($scope.info.f>0){
						var url=SITE_URL+"index.php?app=baseloginapp&act=unfollowtutor&callback=JSON_CALLBACK&id="+$stateParams.id;
					}else{
						var url=SITE_URL+"index.php?app=baseloginapp&act=followtutor&callback=JSON_CALLBACK&id="+$stateParams.id;
					}
					$http.jsonp(url).success( function(data) {
						if(data.done){
							showtoast(data.msg);
							if($scope.info.f>0){
								$scope.info.f=0;
								$scope.info.fnum--;
								if($scope.info.fnum<0){
									$scope.info.fnum=0;
								}
							}else{
								$scope.info.f=1;
								$scope.info.fnum++;
							}
						}
					});	
				}
			}
		}
		$rootScope.media=null;
		$scope.play = function(){
			try{

				console.log($scope.eva.voice_path);
				var vp=$scope.eva.voice_path;
				if(vp!=null&&vp!=''){
					//播放音频 m 即为音频地址
					if($rootScope.media!=undefined&&$rootScope.media!=null){
						$scope.consult.playm="video.png";
						$rootScope.media.release();
						$rootScope.media=null;
					}
					$rootScope.media=audioplay($rootScope.site+vp);
					console.log('newplay');
					console.log($rootScope.media.src);
					$rootScope.media.successCallback=function(){
						$scope.eva.playm="video.png";
						console.log('successCallback png');
						console.log($scope.consult.playm);
						$scope.$apply();
						$rootScope.media=null;
					};
					$rootScope.media.errorCallback=function(err){
						$scope.eva.playm="video.png";
						console.log('successCallback png');
						console.log($scope.consult.playm);
						$scope.$apply();
						$rootScope.media=null;
					};

					$scope.eva.playm="write_d.gif";

					$scope.eva.playeva="video.png";

					console.log('newplay png');
					console.log($scope.eva.playm);
					$rootScope.media.play();

				}
			}
			catch(e){
				console.log('play');
				console.log(e);
			}

		}
		$scope.playeva = function(){
			try{

				console.log($scope.eva.evaluation_voice);
				var vp=$scope.eva.evaluation_voice;
				if(vp!=null&&vp!=''){
					//播放音频 m 即为音频地址
					if($rootScope.media!=undefined&&$rootScope.media!=null){
						$scope.consult.playeva="video.png";
						$rootScope.media.release();
						$rootScope.media=null;
					}
					$rootScope.media=audioplay($rootScope.site+vp);
					console.log('newplay');
					console.log($rootScope.media.src);
					$rootScope.media.successCallback=function(){
						$scope.eva.playeva="video.png";
						console.log('successCallback png');
						$scope.$apply();
						$rootScope.media=null;
					};
					$rootScope.media.errorCallback=function(err){
						$scope.eva.playeva="video.png";
						console.log('errorCallback png');
						$scope.$apply();
						$rootScope.media=null;
					};

					$scope.eva.playeva="write_d.gif";

					$scope.eva.playm="video.png";

					console.log('newplay png');
					console.log($scope.eva.playm);
					$rootScope.media.play();

				}
			}
			catch(e){
				console.log('play');
				console.log(e);
			}
		}

		$scope.share = false;
		$scope.share_show = function(s){
			$scope.share = true;
		}
		/*分享隐藏*/
		$scope.share_hide = function(){
			$scope.share = false;
		}
		
		$scope.wxlogintimer = null;
		/*分享朋友圈*/
		$scope.sharep = function(){
			try{
				if(!$rootScope.inswx){
					showtoast("您还没有安装微信");
					return;
				}
				ProgressIndicator.showSimpleWithLabel(false,'正在启动微信...');
				console.log('sharep');
				var id = $scope.info.user_id;
				var message = $scope.info.note;
				var link = $rootScope.site+"index.php?app=tutor&id="+id;
				var t=$scope.info.tutor_name;
				var pic=$scope.info.tutor_index;
				if(pic==""||pic==undefined){
					pic="data/consultant12.png";
				}else{
					pic+=".small.jpg";
				}
				$scope.wxlogintimer = window.setTimeout("ProgressIndicator.hide()", 3000);
				Wechat.share({
					type: Wechat.Type.WEBPAGE,
					message:{
						title:t ,
						description: message,
						url: link,
						thumbData:SITE_URL+pic
					},
					scene:Wechat.Scene.TIMELINE
				}, 
				function () {		
					showtoast("分享成功");
					if ($scope.wxlogintimer != null)
						window.clearTimeout($scope.wxlogintimer);
					ProgressIndicator.hide();				
				}, function (reason) {
					showtoast("分享失败");
					if ($scope.wxlogintimer != null)
						window.clearTimeout($scope.wxlogintimer);
					ProgressIndicator.hide();
					// 分享失败
					console.log(reason);
				}
				);
			}
			catch(e){
				showtoast("分享失败");
				if ($scope.wxlogintimer != null)
					window.clearTimeout($scope.wxlogintimer);
				ProgressIndicator.hide();
				showcatch(e);
			}
		}
		/*分享微信*/
		$scope.sharew = function(){
			try{
				if(!$rootScope.inswx){
					showtoast("您还没有安装微信");
					return;
				}
				console.log('sharew');
				ProgressIndicator.showSimpleWithLabel(false,'正在启动微信...');

				var id = $scope.info.user_id;
				var message = $scope.info.note;
				var link = $rootScope.site+"index.php?app=tutor&id="+id;
				var t=$scope.info.tutor_name;
				var pic=$scope.info.tutor_index;
				if(pic==""||pic==undefined){
					pic="data/consultant12.png";
				}else{
					pic+=".small.jpg";
				}
				$scope.wxlogintimer = window.setTimeout("ProgressIndicator.hide()", 3000);
				Wechat.share({
					type: Wechat.Type.WEBPAGE,
					message:{
						title: t,
						description: message,
						url: link,
						thumbData:SITE_URL+pic
					},
					scene:Wechat.Scene.SESSION
				}, 
				function () {
					showtoast("分享成功");
					if ($scope.wxlogintimer != null)
						window.clearTimeout($scope.wxlogintimer);
					ProgressIndicator.hide();					
				}, function (reason) {
					showtoast("分享失败");
					if ($scope.wxlogintimer != null)
						window.clearTimeout($scope.wxlogintimer);
					ProgressIndicator.hide();	
					// 分享失败
					console.log(reason);
				}
				);
			}
			catch(e){
				showtoast("分享失败");
				if ($scope.wxlogintimer != null)
					window.clearTimeout($scope.wxlogintimer);
				ProgressIndicator.hide();	
				showcatch(e);
			}
		}

	}
	catch (e) {

		showcatch(e);
	}
});
UserTutorindex.UserTutorindexData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}

		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}